<?php
/**
 * Created by IntelliJ IDEA.
 * User: admin
 * Date: 5/10 0010
 * Time: 14:27
 */

namespace app\api\controller;


use think\Controller;
use think\Db;
use think\Request;

class Pos extends Controller
{
    const SIGNKEY='r2wmwbjl2rmjftsh';
    const DEVELOPERID="100435";

    /**
     * Pos constructor.
     * @param Request|null $request
     */
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        if(!$request->isAjax()){
            echo json_encode(["status"=>0,"data"=>"","msg"=>"请求方式错误"]);
            exit();
        }
    }

    /**
     * @return \think\response\Json
     */
    public function poscodelogin(){
        $data=\model("manager")->poscodelogin();
        return json($data);
    }

    /**
     * 获取美团核销地址
     * @return \think\response\Json
     */
    public function getwriteurl(){
        $userid=Index::verifytoken(request()->post("token"));
        if(!$userid){
            return json(["status"=>-1,"data"=>"","msg"=>"用户鉴权失败"]);
        }
        $shopid=model("manager")->where("id",$userid)->column("shopid")[0];
        $tuantoken=model("meituan")->where("shopid",$shopid)->column("tuantoken")[0];
        if(!$tuantoken){
            return json(["status"=>0,"data"=>"","msg"=>"未绑定"]);
        }
        $url='https://open-erp.meituan.com/checkcoupon?signKey='.self::SIGNKEY.'&appAuthToken='.$tuantoken;
        return json(["status"=>1,"data"=>$url]);
    }

    /**
     * 获取最近版本信息
     * 2017年1月10日19:06:58
     */
    public function poseditionnew(){
        $userid=Index::verifytoken(request()->post("token"));
        if(!$userid){
            return json(["status"=>-1,"data"=>"","msg"=>"用户鉴权失败"]);
        }
        $code=input('code');
        //$code=899;
        //获取本地最新版本code
        $res=Db::name('posversion')->order("code","desc")->find();
        $data=array();
        if($res['code']>$code){
            $data['status']=1;
            $data['info']['title']=$res['version'];
            $data['info']['describe']=$res['updatelog'];
            $data['info']['downurl']=$res['downurl'];
        }elseif($res['code'] <= $code){
            $data['status']=0;
            $data['info']="您当前是最新版本";
        }
        return json($data);
    }

    /**
     * 吐槽一下
     * 2016年8月16日14:36:22
     */
    public function tcyx(){
        if(\request()->isAjax()){
            $userid=Index::verifytoken(request()->post("token"));
            if(!$userid){
                return json(["status"=>-1,"data"=>"","msg"=>"用户鉴权失败"]);
            }
            $res=Db::name("feedback")->insert(array(
                "userid"=>$userid,
                "question"=>input("question"),
                "pic"=>  serialize(input("img")),
                "deviceinfo"=>input("sys"),
                "createtime"=>time()
            ));
            if($res){
                return json(['status' => 1,'msg'  => "发表成功"]);
            }else{
                return json(['status' => 0,'msg'  => "发表失败"]);
            }
        }
    }

}